<?php

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\brand\Brand as mBrand;
use Biz\model\brand\BrandRenewPackageRights as mBrandRenewPackageRights;
use Biz\model\brandRights\BrandPackageRights as mBrandPackageRights;
use Biz\model\brandRights\BrandRightsAll as mBrandRightsAll;
use Biz\model\brandRights\BrandRightsRecored as mBrandRightsRecored;


$page = 1;

$pageSize = 200;

$where = [
    'status' => 1,
];
$where['ORDER'] = ['id' => 'ASC'];

do {
    $limit = [($page - 1) * $pageSize, $pageSize];
    $where['LIMIT'] = $limit;
    $brands = mBrand::select('*', $where);

    foreach ($brands as $brand) {
        $brandId = $brand['id'];
        $brandPackageRights = mBrandPackageRights::selectList(['brand_id' => $brandId, 'rights_status' => 1]);
        foreach ($brandPackageRights as $item) {
            $endTime = strtotime(date('Y-m-d 23:59:59', $item['end_time']));
            mBrandPackageRights::update(['end_time' => $endTime], ['id' => $item['id']]);
            var_dump($item,mBrandPackageRights::log());
        }
        $brandRightsAll = mBrandRightsAll::selectList(['brand_id' => $brandId, 'rights_status' => 1]);
        foreach ($brandRightsAll as $item2) {
            $endTime2 = strtotime(date('Y-m-d 23:59:59', $item2['end_time']));
            mBrandRightsAll::update(['end_time' => $endTime2], ['id' => $item2['id']]);
            var_dump($item2,mBrandRightsAll::log());
        }
        $brandReNewPackageRights = mBrandRenewPackageRights::dataSelect(['brand_id'=> $brandId,'rights_status'=>1]);
        foreach ($brandReNewPackageRights as $item3) {
            $endTime3 = strtotime(date('Y-m-d 23:59:59', $item3['end_time']));
            mBrandRenewPackageRights::update(['end_time' => $endTime3], ['id' => $item3['id']]);
            var_dump($item3,mBrandRenewPackageRights::log());
        }
        $brandRightsRecords = mBrandRightsRecored::selectList(['brand_id'=>$brandId]);
        foreach ($brandRightsRecords as $record){
            $upData = [];
            if(!empty($record['source_data'])){
                $sourceData = json_decode($record['source_data'],true);
                if($sourceData && is_array($sourceData)){
                    if(isset($sourceData['end_time']) && is_numeric($sourceData['end_time'])){
                        $endTime4 = strtotime(date('Y-m-d 23:59:59', $sourceData['end_time']));
                        $sourceData['end_time'] = $endTime4;
                    }

                    $upData['source_data'] = json_encode($sourceData);
                }
            }
            if(!empty($record['operate_data'])){
                $opData = json_decode($record['operate_data'],true);
                if($opData && is_array($opData)){
                    if(isset($opData['end_time']) && is_numeric($opData['end_time'])){
                        $endTime5 = strtotime(date('Y-m-d 23:59:59', $opData['end_time']));
                        $opData['end_time'] = $endTime5;
                    }

                    $upData['operate_data'] = json_encode($opData);
                }
            }

            if(!empty($upData)) {
                mBrandRightsRecored::update($upData, ['id' => $record['id']]);
                var_dump($record,mBrandRightsRecored::log());
            }

        }
    }

    $page++;
} while (!empty($brands));